package com.nchu.react.webflux.component;

import com.nchu.react.webflux.model.City;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.Random;

/**
 * @author fujianjian
 * @project react-learning
 * @date 2018/9/28 上午10:00
 */
@Slf4j
@Component
public class CityHandler {

    private Random random = new Random();

    public Mono<ServerResponse> get(ServerRequest request) {
        int id = this.random.nextInt(1000);
        City city = City.builder().cityCode("" + id).id(id).cityName("上海" + id).build();
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromObject(city));
    }
}
